package priv.conceit.study.book.thread.chapter2.parts3;

/**
 * 2.3
 * 第二章第三节
 * <p>
 * 线程与线程组
 * 如果线程在创建的时候没有指定ThreadGroup 那么会被加入父线程所在的线程组
 * 还会和父线程拥有同样的优先级，同样的daemon守护进程
 *
 * @author Conceit
 * 2019/6/6 增加方法
 * @since 1.0.0, 2019/6/6
 */
public class ThreadAndThreadGroup {


	public static void main(String[] args) {
		threadGroup();
	}

	/**
	 * 测试线程组
	 * 两个线程 t1指定线程组 t2不指定线程组
	 */
	public static void threadGroup() {
		Thread t1 = new Thread("t1");

		ThreadGroup t2ThreadGroup = new ThreadGroup("threadGroup");
		Thread t2 = new Thread(t2ThreadGroup, "t2");

		//由main方法启动 此group为main group
		ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();

		System.out.println("main thread group-->" + mainThreadGroup.getName());
		//mian 线程上级为system 再次上级为null
		System.out.println("main thread group parent-->" + mainThreadGroup.getParent().getParent());

		//t1线程的 group 是否是main线程group
		System.out.println("t1 threadGroup == main threadGroup-->" + (t1.getThreadGroup() == mainThreadGroup));

		//t2线程的 group 是否是main线程group
		System.out.println("t2 threadGroup == main threadGroup-->" + (t2.getThreadGroup() == mainThreadGroup));
		//t2线程的 group 是否为指定的线程group
		System.out.println("t2 threadGroup == t2ThreadGroup-->" + (t2.getThreadGroup() == t2ThreadGroup));

	}


}
